﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LuaInterface;

namespace Console
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        Lua lua = new Lua();
        public MainWindow()
        {
            InitializeComponent();
            //ConsoleLineStart = Console.Document.ContentEnd;
            
        }

        int ConsoleLineLenght = 0;

        public class StrFunc
        {
            public RichTextBox Console;


            public StrFunc(RichTextBox rich)
            {
                Console = rich;
            }

            public void teste(LuaFunction func) //LuaTable table)
            {
                MessageBox.Show(func.ToString());
                //foreach (DictionaryEntry dd in table)
                //{
                //    MessageBox.Show(dd.Value.ToString());
                //}
            }

            public void print(string s)
            {
                //MessageBox.Show(s);
                //Console.Text += s + "\n";
                //TextRange textRange = new TextRange(Console.Document.ContentStart, Console.Document.ContentEnd);
                //TextPointer start = textRange.End.GetPositionAtOffset(0 - 5); //, LogicalDirection.Forward);
                //TextPointer end = textRange.End.GetPositionAtOffset(0);//, LogicalDirection.Backward);
                //Console.Selection.Select(start, end);
                //Console.Selection.Text = s + "\n>> ";
                //ConsoleLineLenght = 0;
               // Console.CaretPosition = Console.Document.ContentEnd;
                MessageBox.Show(s);
            }

            public void clear()
            {
                //Console.Text = "";
            }
        }

        //TextPointer ConsoleLineStart;
        //TextPointer ConsoleLineEnd;
        
        
        private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            //TextPointer i = Console.Selection.Start;
            //Console.Document += "foo";
            //Console.Selection.Start = i;

            //DocumentPosition pos = Console.CaretPosition;
            //Console.CaretPosition
            //Console.Selection.Text;
           // Console.Document.InsertText(pos, DateTime.Now.ToString("F")); ;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            if (string.IsNullOrEmpty(TextBox.Text))
            {
                return;
            }
            Console.BeginChange();
            if (Console.Selection.Text != string.Empty)
            {
                Console.Selection.Text = string.Empty;
            }
            //TextPointer tp = Console.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
            //TextPointer tp = Console.Document.ContentEnd;
            //TextPointer myTextPointer1 = Console.Selection.Start.GetPositionAtOffset(20);
            //TextPointer myTextPointer2 = Console.Selection.End.GetPositionAtOffset(-10);
            string a = Console.Selection.Text;
            //MessageBox.Show(a);
            //Console.Selection.Select(tp, tp);
            Console.Document.ContentEnd.InsertTextInRun(TextBox.Text);
            //Console.CaretPosition.InsertTextInRun(TextBox.Text);
            Console.CaretPosition = Console.Document.ContentEnd;
            Console.EndChange();
            Keyboard.Focus(Console);

        }

        private void Console_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            //Console.Document.ContentEnd.InsertTextInRun(e.Key.ToString());
            // Backspace
            if (e.Key == Key.Return)
            {

                StrFunc strfunc = new StrFunc(Console);
                //Form1 fom = new Form1();
                //lua.RegisterFunction("DanSays", fom, fom.GetType().GetMethod("DanSays"));
                //lua.RegisterFunction("derivative", strfunc, strfunc.GetType().GetMethod("derivative"));
                lua.RegisterFunction("print", strfunc, strfunc.GetType().GetMethod("print"));
                lua.RegisterFunction("clear", strfunc, strfunc.GetType().GetMethod("clear"));
                lua.RegisterFunction("teste", strfunc, strfunc.GetType().GetMethod("teste"));

                TextRange textRange = new TextRange(Console.Document.ContentStart, Console.Document.ContentEnd);
                TextPointer start = textRange.End.GetPositionAtOffset(0 - ConsoleLineLenght - 2); //, LogicalDirection.Forward);
                TextPointer end = textRange.End.GetPositionAtOffset(-2);//, LogicalDirection.Backward);
                Console.Selection.Select(start, end);
                MessageBox.Show(Console.Selection.Text);
                string Sel = Console.Selection.Text;

                if (Sel.Substring(0, 2) == "> ")
                {
                    Sel = Sel.Substring(2);
                    //MessageBox.Show("porra");
                }

                //richTextBox1.Text = strfunc
                try
                {
                    lua["myvar"] = "a";
                    //lua["testando[1]"] = 1;
                    lua.DoString(Sel);
                    //MessageBox.Show(a[0].ToString());
                    //richTextBox1.Text += lua.DoString(richTextBox2.Text).ToString();
                    //TextRange textRange = new TextRange(Console.Document.ContentStart, Console.Document.ContentEnd);
                    start = textRange.End.GetPositionAtOffset(0 - ConsoleLineLenght - 5); //, LogicalDirection.Forward);
                    end = textRange.End.GetPositionAtOffset(0);//, LogicalDirection.Backward);
                    Console.Selection.Select(start, end);
                    MessageBox.Show(Console.Selection.Text);
                    Console.Selection.Text = "\n>> ";
                    ConsoleLineLenght = 0;
                    Console.CaretPosition = Console.Document.ContentEnd;

                }
                catch (Exception ex)
                {
                    textRange = new TextRange(Console.Document.ContentStart, Console.Document.ContentEnd);
                    start = textRange.End.GetPositionAtOffset(0 - ConsoleLineLenght - 5); //, LogicalDirection.Forward);
                    end = textRange.End.GetPositionAtOffset(0);//, LogicalDirection.Backward);
                    Console.Selection.Select(start, end);
                    MessageBox.Show(Console.Selection.Text);
                    Console.Selection.Text = ex.Message.ToString() + "\n\n>> ";
                    ConsoleLineLenght = 0;
                    Console.CaretPosition = Console.Document.ContentEnd;
                    //richTextBox1.Text += ex.Message.ToString() + "\n";
                }
            }
            else if (e.Key == Key.Back)
            {
                if (Console.Selection.Text == string.Empty)
                {
                    if (ConsoleLineLenght > 0)
                    {
                        ConsoleLineLenght -= 1;
                        e.Handled = false;
                    }
                    //TextPointer SelStart = Console.Selection.Start;
                    //TextPointer SelEnd = Console.Selection.End;
                    //if (SelStart.CompareTo(ConsoleLineStart) == 1)
                    //{
                    //MessageBox.Show(SelStart.CompareTo(ConsoleLineStart).ToString());
                    //MessageBox.Show(SelEnd.CompareTo(ConsoleLineEnd).ToString());
                    //MessageBox.Show(Console.Selection.Text);
                    //}
                    //MessageBox.Show("Backspace");
                }
                else if (Console.Selection.Text.Length <= ConsoleLineLenght)
                {
                    ConsoleLineLenght -= Console.Selection.Text.Length;
                    e.Handled = false;
                }
                else if (Console.Selection.Text.Length > ConsoleLineLenght)
                {
                    TextRange textRange = new TextRange(Console.Document.ContentStart, Console.Document.ContentEnd);
                    TextPointer start = textRange.End.GetPositionAtOffset(0 - ConsoleLineLenght - 2); //, LogicalDirection.Forward);
                    TextPointer end = textRange.End.GetPositionAtOffset(0);//, LogicalDirection.Backward);
                    Console.Selection.Select(start, end);
                    Console.Selection.Text = "";
                    ConsoleLineLenght = 0;
                }

            }
            else
            {
                Console.CaretPosition = Console.Document.ContentEnd;
                //ConsoleLineEnd = Console.Document.ContentEnd;
                ConsoleLineLenght++;
                e.Handled = false;
            }
        }
    }
}
